package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.project.adapter.GeneralPurposePopupWindowAdapter;
import com.cloudsigns.factorymanager2.project.customview.GeneralPurposePoupWindow;
import com.cloudsigns.factorymanager2.project.customview.WorkSheetCategoryPoupWindow;
import com.cloudsigns.factorymanager2.project.iview.IAddMaintenanceOrderView;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.presenter.AddMaintenanceOrderPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.Calendar;
import java.util.Locale;

/**
 * 维修订单
 * Created by tony on 2017/12/12.
 */

public class AddMaintenanceOrderActivity extends Activity implements View.OnClickListener, ItemClickListener, IAddMaintenanceOrderView {
    private View rl_back;
    private TextView tv_title;
    private TextView btn_save;
    private EditText edt__project_name, edt__engineering_name, edt__engineering_address,  edt__maintenance_types,edt__completion_date, edt__problem_description;
    private ImageView img_completion_date,img_maintenance_types_select;
    private GeneralPurposePoupWindow generalPurposePoupWindow;
    private String datas[] = {"电器故障", "质量问题","漏水问题","意外损坏"};
    private AddMaintenanceOrderPresenter addMaintenanceOrderPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_maintenance_order);

        addMaintenanceOrderPresenter = new AddMaintenanceOrderPresenter(this);
        init();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        btn_save = findViewById(R.id.btn_save);
        edt__maintenance_types = findViewById(R.id.edt__maintenance_types);
        edt__project_name = findViewById(R.id.edt__project_name);
        edt__engineering_name = findViewById(R.id.edt__engineering_name);
        edt__engineering_address = findViewById(R.id.edt__engineering_address);
        edt__completion_date = findViewById(R.id.edt__completion_date);
        img_completion_date=findViewById(R.id.img_completion_date);
        edt__problem_description = findViewById(R.id.edt__problem_description);
        img_maintenance_types_select=findViewById(R.id.img_maintenance_types_select);

       // tv_title.setText(getResources().getText(R.string.maintenance_order));
        tv_title.setText("维修");
        rl_back.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        img_maintenance_types_select.setOnClickListener(this);
        img_completion_date.setOnClickListener(this);
    }

    private void save() {
        addMaintenanceOrderPresenter.addEngineeringMaintenancenOrder();
//        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
//        finish();
    }

    /**
     * 维修种类窗口
     */
    private void showMaintanceTypesWindow(View anchor) {
        generalPurposePoupWindow = new GeneralPurposePoupWindow(this, datas);
        generalPurposePoupWindow.setItemClick(this);
        generalPurposePoupWindow.showPopupWindow(anchor);
    }

    // 完成日期
    private void showDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt__completion_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());

        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();
    }



    @Override
    public void itemClick(View view, int position) {
        edt__maintenance_types.setText(datas[position]);
    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            // 返回
            case R.id.rl_back:
                finish();
                break;
                // 保存
            case R.id.btn_save:
                save();
                break;
                // 完成日期
            case R.id.img_completion_date:
                showDateDialog();
                break;
            // 维修种类
            case R.id.img_maintenance_types_select:
                showMaintanceTypesWindow(edt__maintenance_types);
                break;


        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (generalPurposePoupWindow != null) {
            generalPurposePoupWindow.dismiss();
            generalPurposePoupWindow = null;
        }
    }


    @Override
    public CommonEnum.MaintenanceCategory getCategory() {
        String categoryStr = edt__maintenance_types.getText().toString().trim();
        CommonEnum.MaintenanceCategory category = CommonEnum.MaintenanceCategory.ELECTRICAL_PROBLEM;
        if (!TextUtils.isEmpty(categoryStr)) {
            if ("电器故障".equals(categoryStr))
                category = CommonEnum.MaintenanceCategory.ELECTRICAL_PROBLEM;
            else if ("质量问题".equals(categoryStr))
                category = CommonEnum.MaintenanceCategory.QUALITY_ISSUES;
        }
        return category;
    }

    @Override
    public String getContent() {
        return "";
    }

    @Override
    public String getEngineeringName() {
        return edt__engineering_name.getText().toString().trim();
    }

    @Override
    public int getLat() {
        return 90;
    }

    @Override
    public int getLon() {
        return 180;
    }

    @Override
    public String getProjectName() {
        return edt__project_name.getText().toString().trim();
    }

    @Override
    public String getQuestionDesc() {
        return edt__problem_description.getText().toString().trim();
    }

    @Override
    public void addMaintenanceOrderSuccess(String projectId) {
        Toast.makeText(this, "保存成功" + projectId, Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, true);
        setResult(ConstantUtil.CREATED_ORDER, mIntent);
        finish();
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, "保存失败" + desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
